<?php
/**
 * 团检套餐
 *
 * User: kaikaic
 * Date: 2018/11/1
 * Time: 21:18
 */

namespace app\exam\controller;

use app\exam\service\GroupSetService;
use app\exam\service\MecItemService;
use app\exam\service\MecService;
use app\exam\service\MecSetService;
use controller\BasicAdmin;
use think\Db;

class GroupSet extends BasicAdmin {

    protected $table = "ExamGroupSet";

    /**
     * 体检套餐-列表
     */
    public function index() {
        $this->title = "团检套餐";

        $query = array();
        $query['name'] = $this->request->get("name");
        $query['mec_id'] = intval($this->request->get("mec_id"));
        $query['status'] = intval($this->request->get("status", 1));

        $db = Db::name($this->table);
        if ($query['name']) {
            $db->whereLike('name', "%{$query['name']}%");
        }
        if ($query['mec_id']) {
            $db->where(['mec_id'=>$query['mec_id']]);
        }
        if ($query['status']) {
            $db->where(['status'=>$query['status']]);
        }

        return parent::listRecord($db);
    }

    /**
     * 体检套餐-新增
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            return view('form', [
                'title'=>'新增团检套餐',
                'vo'=>$vo,
                'mecs'=>MecService::getInstance()->listMec(),
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertAction($data)) {
                $this->showSuccess('添加成功！', "/exam/groupSet/index");
            }else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检套餐-编辑
     */
    public function edit() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $set = GroupSetService::getInstance()->find($id);
            if (empty($set)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑团检套餐',
                'vo'=>$set,
                'mecs'=>MecService::getInstance()->listMec(),
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->showSuccess('编辑成功！', "/exam/groupSet/index");
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检套餐-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $set = GroupSetService::getInstance()->find($id);
        if (empty($set)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = GroupSetService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 设置体检套餐-项目内容
     */
    public function item() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $set = GroupSetService::getInstance()->find($id);
            if (empty($set)) {
                $this->error('记录不存在，请重新操作!');
            }
            $set['item_ids'] = unserialize($set['item_ids']);

            $items = MecItemService::getInstance()->listClassItems();
            for ($i=0; $i<count($items); $i++) {
                $items[$i]['items'] = array_chunk($items[$i]['items'], 3);
            }

            return view('item', [
                'title'=>'编辑体检套餐',
                'vo'=>$set,
                'items'=>$items
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->showSuccess('编辑成功！', "/exam/groupSet/index");
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data, $ids = []) {
        if (empty($data['id'])) {
            $data['item_ids'] = serialize(array());
            $data['sort'] = GroupSetService::getInstance()->getMaxSort() + 1;
            $data['status'] = 1;
            $data['create_time'] = time();

            $result = GroupSetService::getInstance()->insert($data);
        } else {
            $id = intval($data['id']);
            if (isset($data['items'])) {
                $data['item_ids'] = serialize($data['items']);
                unset($data['items']);
            }

            $result = GroupSetService::getInstance()->update($data, $id);
        }

        return $result;
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);

        $mecList = MecService::getInstance()->listMec();
        for ($i=0; $i<$size; $i++) {
            $data[$i]['mec_name'] = $mecList[$data[$i]['mec_id']];

            $items = unserialize($data[$i]['item_ids']);
            $data[$i]['item_num'] = count($items);
        }

        $this->assign('mec_list', $mecList);
    }
}

